package com.loan.system.create.domain;

import cn.hutool.json.JSONUtil;
import com.loan.common.annotation.Excel;
import com.loan.common.core.domain.BaseEntity;
import com.loan.system.domain.work.domain.CreatePdfReq;
import com.loan.system.domain.work.domain.JhVo;
import com.loan.system.domain.work.domain.NhVo;
import com.loan.system.domain.work.domain.WxVo;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * PDF文件生成记录对象 assist_pdf_create_file
 *
 * @author Sophia
 * @date 2024-08-12
 */
@Data
public class AssistPdfCreateFile extends BaseEntity {
    private static final Long serialVersionUID = 1L;

    /**
     * 主键ID
     */
    private Long id;

    /**
     * 所属类型
     */
    @Excel(name = "所属类型")
    private Long typeId;

    /**
     * 户名
     */
    @Excel(name = "户名")
    private String userName;

    /**
     * 账户号
     */
    @Excel(name = "账户号")
    private String accountNo;

    /**
     * 电子流水单
     */
    @Excel(name = "电子流水单")
    private String serialNo;

    /**
     * 开始时间
     */
    @Excel(name = "开始时间")
    private String startTime;

    /**
     * 结束时间
     */
    @Excel(name = "结束时间")
    private String endTime;

    /**
     * 原始金额
     */
    @Excel(name = "原始金额")
    private Integer originalMoney;

    /**
     * 明细总条数
     */
    @Excel(name = "明细总条数")
    private Integer detailNums;

    /**
     * 收入总条数
     */
    @Excel(name = "收入总条数")
    private Integer inNum;

    /**
     * 总金额(收)
     */
    @Excel(name = "总金额(收)")
    private Integer totalInMoney;

    /**
     * 总金额(支)
     */
    @Excel(name = "总金额(支)")
    private Integer totalOutMoney;

    /**
     * 收入明细
     */
    @Excel(name = "收入明细")
    private String inMoneyDetail;

    /**
     * 收支差值
     */
    @Excel(name = "收支差值")
    private String totalDiffMoney;

    /**
     * 支出明细
     */
    @Excel(name = "支出明细")
    private String outMoneyDetail;

    /**
     * pdf文件名称
     */
    @Excel(name = "pdf文件名称")
    private String pdfFileName;

    /**
     * pdf文件下载地址
     */
    @Excel(name = "pdf文件下载地址")
    private String pdfFileDownloadUrl;

    /**
     * 赋值
     *
     * @param req
     */
    public void setNhDataByReq(CreatePdfReq req) {
        this.typeId = req.getTypeId();
        this.detailNums = req.getDetailNums();
        this.startTime = req.getStartTime();
        this.endTime = req.getEndTime();
        this.originalMoney = req.getOriginalMoney();
        this.inNum = req.getInNum();
        this.totalInMoney = req.getTotalInMoney();
        this.totalOutMoney = req.getTotalOutMoney();
        this.userName = req.getUserName();
        this.accountNo = req.getAccountNo();
        this.serialNo = req.getSerialNo();
        this.inMoneyDetail = JSONUtil.toJsonStr(req.getInValueList());
        this.outMoneyDetail = JSONUtil.toJsonStr(req.getOutValueList());
        if (StringUtils.isNotBlank(this.accountNo)) {
            this.setPdfFileName();
        }
    }

    /**
     * 赋值
     *
     * @param nhVo
     */
    public void setNhDataByReq(NhVo nhVo) {
        this.typeId = nhVo.getTypeId();
        this.detailNums = nhVo.getSortDataList().size();
        this.startTime = nhVo.getStartTime();
        this.endTime = nhVo.getEndTime();
        this.originalMoney = nhVo.getOriginalMoney();
        this.totalInMoney = nhVo.getTotalInMoney();
        this.totalOutMoney = nhVo.getTotalOutMoney();
        this.userName = nhVo.getUserName();
        this.accountNo = nhVo.getAccountNo();
        this.serialNo = nhVo.getSerialNo();
        this.inMoneyDetail = JSONUtil.toJsonStr(nhVo.getInValueList());
        this.outMoneyDetail = JSONUtil.toJsonStr(nhVo.getOutValueList());
        if (StringUtils.isNotBlank(this.accountNo)) {
            this.setPdfFileName();
        }
        // 有值且为0时则为新文件;
        this.inNum = 0;
        // 设置差值
        this.totalDiffMoney = String.valueOf(this.totalInMoney - this.totalOutMoney);
    }


    /**
     * 赋值
     *
     * @param jhVo
     */
    public void setJhDataByReq(JhVo jhVo) {
        this.typeId = jhVo.getTypeId();
        this.startTime = jhVo.getStartTime();
        this.endTime = jhVo.getEndTime();
        this.originalMoney = jhVo.getOriginalMoney();
        this.totalInMoney = jhVo.getTotalInMoney();
        this.totalOutMoney = jhVo.getTotalOutMoney();
        this.userName = jhVo.getUserName();
        this.accountNo = jhVo.getAccountNo();
        this.detailNums = jhVo.getSortDataList().size();
        this.inMoneyDetail = JSONUtil.toJsonStr(jhVo.getInValueList());
        this.outMoneyDetail = JSONUtil.toJsonStr(jhVo.getOutValueList());
        if (StringUtils.isNotBlank(this.accountNo)) {
            this.setPdfFileName();
        }
        // 有值且为0时则为新文件;
        this.inNum = 0;
        // 设置差值
        this.totalDiffMoney = String.valueOf(this.totalInMoney - this.totalOutMoney);
    }

    /**
     * 赋值
     *
     * @param wxVo
     */
    public void setWxDataByReq(WxVo wxVo) {
        this.typeId = wxVo.getTypeId();
        this.detailNums = wxVo.getSortDataList().size();
        this.startTime = wxVo.getStartTime();
        this.endTime = wxVo.getEndTime();
        this.originalMoney = wxVo.getOriginalMoney();
        this.totalInMoney = wxVo.getTotalInMoney();
        this.totalOutMoney = wxVo.getTotalOutMoney();
        this.userName = wxVo.getUserName();
        this.accountNo = wxVo.getCode();
        this.serialNo = wxVo.getIdCode();
        this.inMoneyDetail = JSONUtil.toJsonStr(wxVo.getInValueList());
        this.outMoneyDetail = JSONUtil.toJsonStr(wxVo.getOutValueList());
        if (StringUtils.isNotBlank(this.accountNo)) {
            this.setPdfFileName();
        }
        // 有值且为0时则为新文件;
        this.inNum = 0;
        // 设置差值
        this.totalDiffMoney = String.valueOf(this.totalInMoney - this.totalOutMoney);
    }

    /**
     * 设置PDF文件名称
     *
     * @return
     */
    public void setPdfFileName() {
        this.pdfFileName = String.format("%s_%s-%s-(%s_%s).pdf", this.typeId, this.userName, this.accountNo, this.startTime, this.endTime);
    }

    /**
     * 设置PDF文件所在路径
     *
     * @param pdfFilePath
     */
    public void setPdfFilePath(String pdfFilePath) {
        String fileName = StringUtils.endsWithAny(pdfFilePath, "/", "\\") ? this.pdfFileName : "/" + this.pdfFileName;
        this.pdfFileDownloadUrl = String.format("%s%s", pdfFilePath, fileName);
    }

    public String getPdfFilePath() {
        return this.pdfFileDownloadUrl;
    }
}
